<template>
	<vk-data-dialog
		v-model="value.show"
		:title="page.title"
		:top="page.top"
		:width="page.width"
		:close-on-click-modal="true"
		mode="form"
		@open="onOpen"
		@closed="onClose"
	>
		<!-- 页面主体内容开始 -->
		<vk-data-form
			ref="form1"
			v-model="form1.data"
			:form-type="value.mode"
			:rules="form1.props.rules"
			:action="form1.props.action"
			:columns="form1.props.columns"
			:loading.sync="form1.props.loading"
			:labelWidth="form1.props.labelWidth"
			:show-cancel="page.showCancel"
			:cancel-text="page.cancelText"
			:submit-text="page.submitText"
			@success="onFormSuccess"
		></vk-data-form>
		<!-- 页面主体内容结束 -->
	</vk-data-dialog>

</template>

<script>
var that; // 当前页面对象
var vk = uni.vk; // vk实例
var fxLvData = vk.getVuex("$user.mchInfo.fx.level_rule");
export default {
	props: {
		value: {
			Type: Object,
			default: function() {
				return {
					show: false,
					mode: "",
					item: {}
				};
			}
		}
	},
	data: function() {
		// 组件创建时,进行数据初始化
		return {
			page: {
				title: "标题",
				submitText: "确定",
				cancelText: "关闭",
				showCancel: true,
				top: "7vh",
				width: "800px"
			},
			form1: {
				// 表单请求数据，此处可以设置默认值
				data: {

				},
				// 表单属性
				props: {
					// 表单请求地址
					action: "admin/business/sys/user.updateInviter",
					// 表单字段显示规则
					columns: [
						{ key: "nickname", title: "昵称", type: "text-view" },
						{
						  key:"mode", title:"类型", type:"radio",
						  itemWidth:80,
						  data:[
						    { value:1, label:"从现有分销员中选择" },
						    { value:2, label:"系统分销员" },
								{ value:3, label:"无" }
						  ]
						},
						{
						  key: "inviter_uid", title: "当前上级", type: "table-select", placeholder:"请选择上级",
						  action:"admin/business/sys/user.getInviterSelectList",
							rowHeight:50,
							dialogWidth:960,
							pageSize:10,
							multiple:false,
							formData:{
								my_id:""
							},
						  columns:[
								{ key:"avatar", title:"头像", type:"avatar",  width:80, imageWidth:35, shape:"square" },
						    { key:"nickname", title:"昵称", type:"text", nameKey:true,
									formatter: function(val, row, column, index) {
										let str = row.nickname;
										if (row.comment) {
											str += `（${row.comment}）`
										}
										return str;
									}
								},
						    { key:"mobile", title:"手机号", type:"text", width:140, },
								{ key:"fx.lv", title:"分销等级", type:"select", width:100, placeholder:"请选择等级", data:fxLvData,
									props:{ value:'lv', label:'name', children:'children' }
								},
								{ key:"_id", title:"id", type:"text", idKey:true, width:250, }, // idKey:true 代表此字段为主键字段，若设置show:["none"],则可以在表格中隐藏该字段的显示
						  ],
						  queryColumns:[
								{ key: "nickname", title: "昵称", type: "text", width: 120, mode: "%%" },
						    { key: "real_name", title: "姓名", type: "text", width: 120, mode: "%%" },
						    { key: "mobile", title: "手机号", type: "text", width: 120, mode: "%%" },
								{ key:"fx_lv", title:"等级", type:"select", width:120, data:fxLvData, fieldName:"fx.lv",
									props:{ value:'lv', label:'name', children:'children' }
								},
						  ],
							showRule:"mode=1"
						}
					],
					// 表单验证规则
					rules: {
						inviter_uid: [
							// 必填
							{ required: true, message: "请选择上级", trigger: ['blur','change'] }
						],
					},
					// 左侧label的宽度
					labelWidth: "100px",
				}
			}
		};
	},
	mounted() {
		that = this;
		that.init();
	},
	methods: {
		// 初始化
		init() {
			let { value } = that;
			that.$emit("input", value);
		},
		// 监听 - 页面打开
		onOpen() {
			that = this;
			let { item={} } = that.value;
			// 每次打开时，重新设置表单的值 = value.item 的值，item通过 vk.pubfn.openForm('表单名',{ item:{ _id:"1" } }) 传递值
			let inviter_uid = item.inviter_uid ? item.inviter_uid[0] : "";
			let inviter_lock = item.inviter_lock;
			let mode = 1;
			if(!inviter_uid){
				if (inviter_lock) {
					mode = 2;
				} else {
					mode = 3;
				}
			}
			that.form1.data = {
				mode,
				_id : item._id,
				nickname : item.nickname,
				inviter_uid : inviter_uid,
				inviter_lock
			};
			that.form1.props.columns[2].formData.my_id = item._id;
		},
		// 监听 - 页面关闭
		onClose() {
			//that.$refs.form1.resetForm(); // 关闭时，重置表单
		},
		// 监听 - 提交成功后
		onFormSuccess() {
			that.value.show = false; // 关闭页面
			that.$emit("success");
		}
	},
	watch: {

	},
	// 计算属性
	computed: {}
};
</script>

<style lang="scss" scoped></style>
